import CommentService from '~/server/database/services/commentService'

export default defineEventHandler(async (event) => {
  try {
    const query = getQuery(event)
    const { postId, page = 1, pageSize = 10 } = query

    if (!postId) {
      throw createError({
        statusCode: 400,
        statusMessage: 'Bad Request',
        data: { message: '帖子ID不能为空' }
      })
    }

    const result = await CommentService.getCommentsByPostId(
      parseInt(postId as string),
      parseInt(page as string),
      parseInt(pageSize as string)
    )

    return {
      success: true,
      data: result,
      message: '获取评论列表成功'
    }
  } catch (error: unknown) {
    console.error('获取评论列表失败:', error)
    
    if (error && typeof error === 'object' && 'statusCode' in error) {
      throw error
    }
    
    throw createError({
      statusCode: 500,
      statusMessage: 'Internal Server Error',
      data: { message: '获取评论列表失败' }
    })
  }
})
